package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.service.MarketAdminAddressService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketAdminAdminAddressServiceImpl implements MarketAdminAddressService {
    @Override
    public List<MarketAddress> list(int page, int limit, String name, Integer userId, String sort, String order) {
        // 获取session和example
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAddressMapper mapper = sqlSession.getMapper(MarketAddressMapper.class);
        MarketAddressExample example = new MarketAddressExample();

        MarketAddressExample.Criteria criteria = example.or();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId!=null) {
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(sort + " " + order);

        // 设置PageHelper,并执行查询
        PageHelper.startPage(page,limit);
        List<MarketAddress> addresses = mapper.selectByExample(example);

        // 提交并关闭sqlSession
        sqlSession.commit();
        sqlSession.close();

        return  addresses;
    }
}
